<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<script src="../../__/import.js"></script>
<title>修改密码</title>
</head>
<style>
#vm {
	width: 100%;
	height: 100%;
}
</style>
<body>
	<div id="vm" v-cloak class="form-wrap bg">
		<form is="el-form" :modle="form" ref="form" style="padding-top: 12px;">
			<div is="el-form-item" label="旧登录密码" label-width="88px">
				<div is="el-input" type="password" v-model.trim="form.oldPassword" placeholder="请输入旧登录密码" clearable></div>
			</div>
			<div is="el-form-item" label="新登录密码" label-width="88px">
				<div is="el-input" type="password" v-model.trim="form.newPassword" placeholder="请输入新登录密码" clearable></div>
			</div>
			<div is="el-form-item" label="确认新密码" label-width="88px">
				<div is="el-input" type="password" v-model.trim="form.newPassword2" placeholder="请再次输入新登录密码" clearable></div>
			</div>
			<div is="el-form-item" align="right">
				<button is="el-button" @click="confirm()">确认</button>
				<button is="el-button" @click="cancel()">取消</button>
			</div>
		</form>
	</div>

	<script>
		new Vue({
			el : '#vm',
			data : {
				form : {
					oldPassword : '',
					newPassword : '',
					newPassword2 : '',
				}
			},
			created : function() {
				__.$vm = this;
			},
			mounted : function() {
				//__.mask();
			},
			methods : {
				confirm : function(done) {
					var vm = this, form = vm.form;
					if (__.isEmpty(form.oldPassword)) {
						__.message({
							message : '请输入旧密码',
							type : 'warning'
						});
						return;
					}
					if (__.isEmpty(form.newPassword)) {
						__.message({
							message : '请输入新密码',
							type : 'warning'
						});
						return;
					}
					if (__.isEmpty(form.newPassword2)) {
						__.message({
							message : '请再次输入新密码',
							type : 'warning'
						});
						return;
					}
					if (form.newPassword != form.newPassword2) {
						__.message({
							message : '两次新密码输入不一致',
							type : 'warning'
						});
						return;
					}
					__.mask();
					__.api('POST', '/rest/auth/getPublicKey', {}, function(result) {
						if (__.err(result)) {
							__.unmask();
							__.message({
								message : result.message || '秘钥获取失败',
								type : 'warning'
							});
							return;
						}
						var rsaKey = result.data;
						var encrypt = new JSEncrypt();
						encrypt.setPublicKey(rsaKey.publicKey);
						var params = {};
						params.oldPassword = encrypt.encrypt(form.oldPassword);
						params.newPassword = encrypt.encrypt(form.newPassword);
						params.rsaId = rsaKey.rsaId;
						__.api('POST', '/rest/user/updateCurrentPassword', params, function(result) {
							__.unmask();
							if (__.err(result)) {
								__.message({
									message : result.message,
									type : 'warning'
								});
								return;
							}
							__.message({
								message : '密码修改成功',
								type : 'success'
							});
							__.dialog.close(1);
						});
					});
				},
				cancel : function() {
					__.dialog.close(0);
				}
			}
		});
	</script>
</body>
</html>